import { createStore } from 'vuex'
import http from '@/http'

const router = {
  namespaced: true,
  state: {
    dynamicRouter: [],
    isRequested: false
  },
  mutations: {
    changeRouter(state: any, payload: any) {
      state.dynamicRouter = payload
    },
    changeRequesed(state: any, payload: boolean) {
      state.isRequested = payload
    }
  },
  actions: {
    async asyncRequestRouter({ commit }: { commit: (name: string, payload: any) => void }) {
      return new Promise((resolve, reject) => {
        http.get('/api/router').then(res => {
          commit('changeRouter', res.data.router)
          commit('changeRequesed', true)
          resolve(res.data.router)
        }).catch(err => {
          console.log(err, 'err')
        })
      })
    }
  }

}

export default createStore({
  state: {
  },
  getters: {
  },
  mutations: {
  },
  actions: {
  },
  modules: {
    router
  }
})
